/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Golf_Manager;

import javax.swing.*;
import Golf_Manager.model.MemberMngModel;
import Golf_Manager.vo.Member;
import Golf_Manager.editor.*;
import Golf_Manager.editor.doc.IntegerDocument;
import Golf_Manager.editor.doc.PositiveIntegerDocument;
import Golf_Manager.editor.doc.TextLimitDocument;
import Golf_Manager.sqlite.MemberDAO;
import Golf_Manager.sqlite.sql.MemberSql;
import Golf_Manager.vo.ValidVo;
import com.toedter.calendar.IDateEditor;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.im.InputContext;
import java.awt.im.InputSubset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;

/**
 *
 * @author Administrator
 */
public class MemberMngFrame extends javax.swing.JInternalFrame {
    
    /**
     * Creates new form NewJInternalFrame
     */
    public MemberMngFrame() {
        initComponents(); 
        setjTableDataModel();
        meeberSearch();
    }
    private void setjTableDataModel(){
        //searchBirth 포커스 이벤트 설정
        IDateEditor editor = searchBirth.getDateEditor();
        JComponent comp = editor.getUiComponent();
        comp.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                if(searchBirth.getDate() == null){
                    searchBirth.setDate(null);
                } 
            }
        });

        //CELL 0설정 시작
        JTextField jfName = new JTextField();
        jfName.setDocument(new TextLimitDocument(15));
        jfName.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                super.focusGained(e); //To change body of generated methods, choose Tools | Templates.
                JTextField eventJfName = (JTextField)e.getSource();
                InputContext icx  = eventJfName.getInputContext();
                if(icx != null)
                    eventJfName.getInputContext().setCharacterSubsets(new Character.Subset[]{InputSubset.HANJA });
            }

            @Override
            public void focusLost(FocusEvent e) {
                super.focusLost(e); //To change body of generated methods, choose Tools | Templates.
                 memberTable.getColumnModel().getColumn(0).getCellEditor().stopCellEditing();
            }
            
            
        });
        memberTable.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(jfName));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        memberTable.getColumnModel().getColumn(0).setCellRenderer(renderer);
        memberTable.getColumnModel().getColumn(0).setPreferredWidth(150);
        //CELL 0설정 끝
        
        //CELL 1설정 시작
        memberTable.getColumnModel().getColumn(1).setCellEditor(new CellJDateChooserEditor());        
        memberTable.getColumnModel().getColumn(1).setPreferredWidth(80);
        //CELL 1설정 끝
        
        //CELL 2설정 시작
        JTextField jfAge = new JTextField();
        jfAge.setDocument(new PositiveIntegerDocument (3));
        jfAge.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                super.focusGained(e); //To change body of generated methods, choose Tools | Templates.
                JTextField eventJfAge = (JTextField)e.getSource();
                InputContext icx  = eventJfAge.getInputContext();
                if(icx != null)
                    eventJfAge.getInputContext().setCharacterSubsets(new Character.Subset[]{InputSubset.HANJA });
            }

            @Override
            public void focusLost(FocusEvent e) {
                super.focusLost(e); //To change body of generated methods, choose Tools | Templates.
                 memberTable.getColumnModel().getColumn(2).getCellEditor().stopCellEditing();
            }
            
            
        });
        memberTable.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(jfAge));
        renderer = new DefaultTableCellRenderer();
        memberTable.getColumnModel().getColumn(2).setCellRenderer(renderer);
        memberTable.getColumnModel().getColumn(2).setPreferredWidth(50);
        //CELL 2설정 끝
        
        //CELL 3설정 시작
        JComboBox comboBoxGender = new JComboBox();
        comboBoxGender.addItem("남");
        comboBoxGender.addItem("여");
        memberTable.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(comboBoxGender));
        renderer = new DefaultTableCellRenderer();
        memberTable.getColumnModel().getColumn(3).setCellRenderer(renderer);
        memberTable.getColumnModel().getColumn(3).setPreferredWidth(50);
        //CELL 3설정 끝
        
        //CELL 4설정 시작
        JTextField jfCompanyName = new JTextField();
        jfCompanyName.setDocument(new TextLimitDocument(15));
        jfCompanyName.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                super.focusGained(e); //To change body of generated methods, choose Tools | Templates.
                JTextField eventJfCompanyName = (JTextField)e.getSource();
                InputContext icx  = eventJfCompanyName.getInputContext();
                if(icx != null)
                    eventJfCompanyName.getInputContext().setCharacterSubsets(new Character.Subset[]{InputSubset.HANJA });
            }

            @Override
            public void focusLost(FocusEvent e) {
                super.focusLost(e); //To change body of generated methods, choose Tools | Templates.
                memberTable.getColumnModel().getColumn(4).getCellEditor().stopCellEditing();
            }
            
            
        });
        memberTable.getColumnModel().getColumn(4).setCellEditor(new DefaultCellEditor(jfCompanyName));
        renderer = new DefaultTableCellRenderer();
        memberTable.getColumnModel().getColumn(4).setCellRenderer(renderer);
        memberTable.getColumnModel().getColumn(4).setPreferredWidth(200);
        //CELL 4설정 끝
        
        //CELL 5설정 시작
        JTextField jfHand = new JTextField();
        jfHand.setDocument(new IntegerDocument (3));
        jfHand.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                super.focusGained(e); //To change body of generated methods, choose Tools | Templates.
                JTextField eventJfHand = (JTextField)e.getSource();
                InputContext icx  = eventJfHand.getInputContext();
                if(icx != null)
                    eventJfHand.getInputContext().setCharacterSubsets(new Character.Subset[]{InputSubset.HANJA });
            }

            @Override
            public void focusLost(FocusEvent e) {
                super.focusLost(e); //To change body of generated methods, choose Tools | Templates.
                memberTable.getColumnModel().getColumn(5).getCellEditor().stopCellEditing();
            }
            
            
        });
        memberTable.getColumnModel().getColumn(5).setCellEditor(new DefaultCellEditor(jfHand));
        renderer = new DefaultTableCellRenderer();
        memberTable.getColumnModel().getColumn(5).setCellRenderer(renderer);
        memberTable.getColumnModel().getColumn(5).setPreferredWidth(50);
        //CELL 5설정 끝
        
        //CELL 6설정 시작
        JComboBox comboBoxGrade = new JComboBox();
        comboBoxGrade.addItem("정회원");
        comboBoxGrade.addItem("비회원");
        memberTable.getColumnModel().getColumn(6).setCellEditor(new DefaultCellEditor(comboBoxGrade));
        renderer = new DefaultTableCellRenderer();
        memberTable.getColumnModel().getColumn(6).setCellRenderer(renderer);
        memberTable.getColumnModel().getColumn(6).setPreferredWidth(60);
        //CELL 6설정 끝
        
        //CELL 7설정 시작
        JComboBox JoinYn = new JComboBox();
        JoinYn.addItem("참여");
        JoinYn.addItem("불참");
        memberTable.getColumnModel().getColumn(7).setCellEditor(new DefaultCellEditor(JoinYn));
        renderer = new DefaultTableCellRenderer();
        memberTable.getColumnModel().getColumn(7).setCellRenderer(renderer);
        memberTable.getColumnModel().getColumn(7).setPreferredWidth(50);
        //CELL 7설정 끝
        
        //CELL 8설정 시작
        JTextField jfNote1 = new JTextField();
        jfNote1.setDocument(new TextLimitDocument(10));
        jfNote1.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                super.focusGained(e); //To change body of generated methods, choose Tools | Templates.
                JTextField eventJfNote1 = (JTextField)e.getSource();
                InputContext icx  = eventJfNote1.getInputContext();
                if(icx != null)
                    eventJfNote1.getInputContext().setCharacterSubsets(new Character.Subset[]{InputSubset.HANJA });
            }

            @Override
            public void focusLost(FocusEvent e) {
                super.focusLost(e); //To change body of generated methods, choose Tools | Templates.
                memberTable.getColumnModel().getColumn(8).getCellEditor().stopCellEditing();
            }
            
            
        });
        memberTable.getColumnModel().getColumn(8).setCellEditor(new DefaultCellEditor(jfNote1));
        renderer = new DefaultTableCellRenderer();
        memberTable.getColumnModel().getColumn(8).setCellRenderer(renderer);
        memberTable.getColumnModel().getColumn(8).setPreferredWidth(50);
        //CELL 8설정 끝
        
        //CELL 9설정 시작
        JTextField jfNote2 = new JTextField();
        jfNote2.setDocument(new TextLimitDocument(10));
        jfNote2.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                super.focusGained(e); //To change body of generated methods, choose Tools | Templates.
                JTextField eventJfNote2 = (JTextField)e.getSource();
                InputContext icx  = eventJfNote2.getInputContext();
                if(icx != null)
                    eventJfNote2.getInputContext().setCharacterSubsets(new Character.Subset[]{InputSubset.HANJA });
            }

            @Override
            public void focusLost(FocusEvent e) {
                super.focusLost(e); //To change body of generated methods, choose Tools | Templates.
                memberTable.getColumnModel().getColumn(9).getCellEditor().stopCellEditing();
            }
            
            
        });
        memberTable.getColumnModel().getColumn(9).setCellEditor(new DefaultCellEditor(jfNote2));
        renderer = new DefaultTableCellRenderer();
        memberTable.getColumnModel().getColumn(9).setCellRenderer(renderer);
        memberTable.getColumnModel().getColumn(9).setPreferredWidth(50);
        //CELL 9설정 끝
        
        //CELL 10설정 시작
        JTextField jfNote3 = new JTextField();
        jfNote3.setDocument(new TextLimitDocument(10));
        jfNote3.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                super.focusGained(e); //To change body of generated methods, choose Tools | Templates.
                JTextField eventJfNote3 = (JTextField)e.getSource();
                InputContext icx  = eventJfNote3.getInputContext();
                if(icx != null)
                    eventJfNote3.getInputContext().setCharacterSubsets(new Character.Subset[]{InputSubset.HANJA });
            }

            @Override
            public void focusLost(FocusEvent e) {
                super.focusLost(e); //To change body of generated methods, choose Tools | Templates.
                memberTable.getColumnModel().getColumn(10).getCellEditor().stopCellEditing();
            }
            
            
        });
        memberTable.getColumnModel().getColumn(10).setCellEditor(new DefaultCellEditor(jfNote3));
        renderer = new DefaultTableCellRenderer();
        memberTable.getColumnModel().getColumn(10).setCellRenderer(renderer);
        memberTable.getColumnModel().getColumn(10).setPreferredWidth(50);
        //CELL 10설정 끝
        
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        searchName = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        memberTable = new javax.swing.JTable(new MemberMngModel());
        memberDel = new javax.swing.JButton();
        memberSearch = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        memberSave = new javax.swing.JButton();
        memberAdd = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        searchBirth = new com.toedter.calendar.JDateChooser();
        jLabel3 = new javax.swing.JLabel();
        searchResult = new javax.swing.JLabel();

        setTitle("회원관리");
        setPreferredSize(new java.awt.Dimension(1024, 768));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("이름");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("생년월일");

        searchName.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                searchNameFocusGained(evt);
            }
        });

        memberTable.setRowHeight(20);
        memberTable.getTableHeader().addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int col = memberTable.columnAtPoint(e.getPoint());
                TableColumnModel cmodel = memberTable.getColumnModel();

                for(int headColIndex = 0; headColIndex < cmodel.getColumnCount(); headColIndex++){
                    if(col != headColIndex)
                    cmodel.getColumn(headColIndex).setHeaderValue(cmodel.getColumn(headColIndex).getHeaderValue().toString().replace("▲", "").replace("▼", ""));
                }

                if(cmodel.getColumn(col).getHeaderValue().toString().contains("▲")) {
                    //order가 ASC(▲)인 경우 DESC(▼)
                    cmodel.getColumn(col).setHeaderValue(cmodel.getColumn(col).getHeaderValue().toString().replace("▲", "▼"));
                    meeberOrderSearch(MemberSql.getTableColumnNm(col).toString().concat(" DESC"));
                } else if(cmodel.getColumn(col).getHeaderValue().toString().contains("▼"))  {
                    //order가 DESC(▼)인 경우 ASC(▲)
                    cmodel.getColumn(col).setHeaderValue(cmodel.getColumn(col).getHeaderValue().toString().replace("▼", "▲"));
                    meeberOrderSearch(MemberSql.getTableColumnNm(col).toString().concat(" ASC"));
                } else {
                    //order가 없는경우
                    cmodel.getColumn(col).setHeaderValue(cmodel.getColumn(col).getHeaderValue().toString().concat("▲"));
                    meeberOrderSearch(MemberSql.getTableColumnNm(col).toString().concat(" ASC"));
                }
                //memberTable.getTableHeader().repaint();
            }
        });
        DefaultListSelectionModel selectionModel = (DefaultListSelectionModel) memberTable.getSelectionModel();
        selectionModel.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                if(e.getValueIsAdjusting()){
                    int selCount = 0;
                    MemberMngModel memberMdl = (MemberMngModel)memberTable.getModel();
                    DefaultListSelectionModel selectionModel  = (DefaultListSelectionModel)e.getSource();
                    for(int i= memberMdl.getMembers().size()-1; i>=0 ; i--){
                        if(selectionModel.isSelectedIndex(i)){
                            selCount++;
                        }
                    }
                    searchResult.setText(memberMdl.getMembers().size() + " 건 (선택된 행 " + selCount + "건)");

                }
            }

        });
        jScrollPane1.setViewportView(memberTable);

        memberDel.setText("삭제");
        memberDel.setEnabled(false);
        memberDel.setVisible(false);
        memberDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                memberDelActionPerformed(evt);
            }
        });

        memberSearch.setText("조회");
        memberSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                memberSearchActionPerformed(evt);
            }
        });

        memberSave.setText("저장");
        memberSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                memberSaveActionPerformed(evt);
            }
        });

        memberAdd.setText("추가");
        memberAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                memberAddActionPerformed(evt);
            }
        });

        searchBirth.setDateFormatString("yyyy-MM-dd");

        jLabel3.setText("조회결과 :");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(167, 167, 167)
                                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 581, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(searchName, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(searchBirth, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(memberSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 248, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(searchResult)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(memberDel, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(memberAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(memberSave, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.LEADING))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(searchName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addComponent(memberSearch)
                    .addComponent(searchBirth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 3, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(memberDel)
                    .addComponent(memberSave)
                    .addComponent(memberAdd)
                    .addComponent(jLabel3)
                    .addComponent(searchResult))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 563, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void meeberSearch(){ 
        
        TableColumnModel cmodel = memberTable.getColumnModel();
        for(int headColIndex = 0; headColIndex < cmodel.getColumnCount(); headColIndex++){
                cmodel.getColumn(headColIndex).setHeaderValue(cmodel.getColumn(headColIndex).getHeaderValue().toString().replace("▲", "").replace("▼", ""));
        }
        
        MemberMngModel memberMdl = (MemberMngModel)memberTable.getModel();
        DefaultListSelectionModel selectionModel = (DefaultListSelectionModel) memberTable.getSelectionModel();
        
        StringBuilder whrerSql = new StringBuilder("");
        if(searchName.getText() != null && !searchName.getText().equals("")){
            whrerSql.append(" AND NAME LIKE '%").append(searchName.getText()).append("%'");
        } 
        if(searchBirth.getDate() != null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            whrerSql.append(" AND BIRTH = '").append(sdf.format(searchBirth.getDate())).append("'");
        }
        
        memberMdl.setMembers(MemberDAO.selectMember(whrerSql, ""));
        memberTable.repaint();
        
        int selCount = 0;
        for(int i= memberMdl.getMembers().size()-1; i>=0 ; i--){
                if(selectionModel.isSelectedIndex(i)){
                    selCount++;
                }
        }
        if(selCount > 0){
            searchResult.setText(memberMdl.getMembers().size() + " 건 (선택된 행 " + selCount + "건)");   
        } else {
            searchResult.setText(memberMdl.getMembers().size() + " 건");
        }
    }
    
    private void meeberOrderSearch(String orderString){ 
        MemberMngModel memberMdl = (MemberMngModel)memberTable.getModel();
        DefaultListSelectionModel selectionModel = (DefaultListSelectionModel) memberTable.getSelectionModel();
        
        StringBuilder whrerSql = new StringBuilder("");
        if(searchName.getText() != null && !searchName.getText().equals("")){
            whrerSql.append(" AND NAME LIKE '%").append(searchName.getText()).append("%'");
        } 
        if(searchBirth.getDate() != null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            whrerSql.append(" AND BIRTH = '").append(sdf.format(searchBirth.getDate())).append("'");
        }
        
        memberMdl.setMembers(MemberDAO.selectMember(whrerSql, orderString));
        memberTable.repaint();
        
        int selCount = 0;
        for(int i= memberMdl.getMembers().size()-1; i>=0 ; i--){
                if(selectionModel.isSelectedIndex(i)){
                    selCount++;
                }
        }
        if(selCount > 0){
            searchResult.setText(memberMdl.getMembers().size() + " 건 (선택된 행 " + selCount + "건)");   
        } else {
            searchResult.setText(memberMdl.getMembers().size() + " 건");
        }
    }
    
    private void memberSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_memberSearchActionPerformed
        // TODO add your handling code here:
        meeberSearch();
        JOptionPane.showMessageDialog(rootPane, "조회되었습니다.", "조회", WIDTH);
    }//GEN-LAST:event_memberSearchActionPerformed

    private void memberSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_memberSaveActionPerformed
        // TODO add your handling code here:
        
        if(JOptionPane.showConfirmDialog(rootPane, "저장하시겠습니까??", "확인", JOptionPane.YES_NO_OPTION) == 0){
            MemberMngModel memberMdl = (MemberMngModel)memberTable.getModel(); 
            ValidVo validVo = memberMdl.validCheckMember();
            if(validVo != null){
               memberTable.editCellAt(validVo.getRowIndex(), validVo.getColIndex());
               JOptionPane.showMessageDialog(rootPane, validVo.getRowIndex() + " 행의 " + validVo.getMsg(), "확인", WIDTH); 
            } else {
                if(MemberDAO.saveMember(memberMdl.getMembers()) > 0){
                    meeberSearch();
                    JOptionPane.showMessageDialog(rootPane, "저장되었습니다.", "확인", WIDTH);
                } else {
                    JOptionPane.showMessageDialog(rootPane, "수정된 건이 없습니다.", "확인", WIDTH);
                }      

            }  
        }
    }//GEN-LAST:event_memberSaveActionPerformed

    private void memberAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_memberAddActionPerformed
        // TODO add your handling code here:
        //DefaultTableModel model = (DefaultTableModel) memberTable.getModel();
        MemberMngModel memberMdl = (MemberMngModel)memberTable.getModel();
        DefaultListSelectionModel selectionModel = (DefaultListSelectionModel) memberTable.getSelectionModel();
        List<Member> oldMembers = new ArrayList<>(memberMdl.getMembers());
        memberMdl.delMembers();
        memberMdl.addMember(new Member("Y"));
        memberMdl.addMembers(oldMembers);
        memberTable.repaint();
        
        int selCount = 0;
        for(int i= memberMdl.getMembers().size()-1; i>=0 ; i--){
                if(selectionModel.isSelectedIndex(i)){
                    selCount++;
                }
        }
        if(selCount > 0){
            searchResult.setText(memberMdl.getMembers().size() + " 건 (선택된 행 " + selCount + "건)");   
        } else {
            searchResult.setText(memberMdl.getMembers().size() + " 건");
        }
    }//GEN-LAST:event_memberAddActionPerformed

    private void memberDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_memberDelActionPerformed
        // TODO add your handling code here:
        if(memberTable.getSelectedRow() > -1){
            if(JOptionPane.showConfirmDialog(rootPane, "삭제하시겠습니까??", "확인", JOptionPane.YES_NO_OPTION) == 0){
                int del = 0;
                StringBuilder delMemberSeqs = new StringBuilder("");
                MemberMngModel memberMdl = (MemberMngModel)memberTable.getModel();
                DefaultListSelectionModel selectionModel = (DefaultListSelectionModel) memberTable.getSelectionModel();
                for(int i= memberMdl.getMembers().size()-1; i>=0 ; i--){
                    if(selectionModel.isSelectedIndex(i)){
                        if(memberMdl.getMembers().get(i).getMemberSeq() != null && !memberMdl.getMembers().get(i).getMemberSeq().equals("") ){
                            delMemberSeqs.append(", '").append(memberMdl.getMembers().get(i).getMemberSeq()).append("'");
                            memberMdl.delMember(i);
                        } else {
                            memberMdl.delMember(i);
                            del++;
                        }
                    }
                }
                
                if(!delMemberSeqs.toString().equals("")){
                    del += MemberDAO.deleteMember(delMemberSeqs.toString().subSequence(2, delMemberSeqs.length()).toString());
                }
                if(del > 0){
                    meeberSearch();
                    JOptionPane.showMessageDialog(rootPane, del + "행이 삭제되었습니다.", "확인", WIDTH);
                }
            }
        } else {
            JOptionPane.showMessageDialog(rootPane, "삭제 하고자 하는 행을 선택하세요.", "확인", WIDTH);
        }
    }//GEN-LAST:event_memberDelActionPerformed

    private void searchNameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_searchNameFocusGained
        // TODO add your handling code here:
        InputContext icx  = searchName.getInputContext();
        if(icx != null)
            searchName.getInputContext().setCharacterSubsets(new Character.Subset[]{InputSubset.HANJA });
    }//GEN-LAST:event_searchNameFocusGained

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JButton memberAdd;
    private javax.swing.JButton memberDel;
    private javax.swing.JButton memberSave;
    private javax.swing.JButton memberSearch;
    private javax.swing.JTable memberTable;
    private com.toedter.calendar.JDateChooser searchBirth;
    private javax.swing.JTextField searchName;
    private javax.swing.JLabel searchResult;
    // End of variables declaration//GEN-END:variables
}
